package queue;

import linkedlist.LinkedListQueue;

import java.util.Random;

public class QueueTest {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedListQueue<>();
        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
            System.out.println("queue = " + queue);
            if (i % 3 == 2) {
                queue.dequeue();
                System.out.println("queue = " + queue);
            }
        }
        int opCount = 1000000;
//        System.out.println("testQueue(new ArrayQueue<Integer>,opCount) = " + testQueue(new ArrayQueue<Integer>(), opCount));
        System.out.println("testQueue(new ArrayQueue<Integer>,opCount) = " + testQueue(new LoopQueue<Integer>(), opCount));
    }

    /**
     * 测试使用q运行opCount个enqueue和dequeue操作所需要的时间，单位：秒
     *
     * @param q
     * @param opCount
     * @return 秒
     */
    private static double testQueue(Queue<Integer> q, int opCount) {
        long startTime = System.nanoTime();
        Random random = new Random();
        for (int i = 0; i < opCount; i++) {
            q.enqueue(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            q.dequeue();
        }
        long endTime = System.nanoTime();
        return (endTime - startTime) / 1000000000.0;
    }

    ;
}
